﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using WeiBeeCommon.DataTypes;
using FollowersToExcel;
using System.Text.RegularExpressions;
using WeiBeeCommon.Core;
using System.Net;

namespace TestFollowerToExcel
{
    
    
    /// <summary>
    ///This is a test class for Form1Test and is intended
    ///to contain all Form1Test Unit Tests
    ///</summary>
    [TestClass()]
    public class Form1Test
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for GetUserInfoFromUrl
        ///</summary>
        [TestMethod()]
        public void GetUserInfoFromUrlTest()
        {
            Form1 target = new Form1(); 
            string url = @"http://weibo.com/tinyfool";
            SinaUser actual = target.GetUserInfoFromUrl(url);
            Assert.AreEqual("1400229064", actual.id);
        }
        /// <summary>
        ///A test for GetUserInfoFromUrl
        ///</summary>
        [TestMethod()]
        public void GetCorpUserInfoFromUrlTest()
        {
            Form1 target = new Form1(); 
            string url = @"http://weibo.com/quanjinglian";
            SinaUser actual = target.GetUserInfoFromUrl(url);
            Assert.AreEqual("1608195712", actual.id);
        }

                /// <summary>
        ///A test for GetUserInfoFromUrl
        ///</summary>
        [TestMethod()]
        public void SaveExcelTest()
        {
            Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.ApplicationClass();
            app.Visible = false;
            Microsoft.Office.Interop.Excel.Workbooks wbs = app.Workbooks;
            Microsoft.Office.Interop.Excel.Workbook wb = wbs.Add(Type.Missing);
            Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[1];
            Microsoft.Office.Interop.Excel.Range r = ws.get_Range("A1", "H1");
            object[] objHeader = { "标题1", "标题2", "标题3", "标题4", "标题5", "标题6", "标题7", "标题8" };
            r.Value = objHeader;
            ws.Name = "myname";
            wb.SaveAs("abcde.xlsx");
            wb.Close(false, Type.Missing, Type.Missing);
            wbs.Close();
            app.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
            wb = null;
            wbs = null;
            app = null;
            r = null;
            GC.Collect();
        }

        [TestMethod]
        public void UrlToWeiBeeTypeTest()
        {
            string sinaUrl = "http://weibo.com/drshipeng";
            string sohuUrl = "http://zengfuhu.t.sohu.com/";
            Regex sinaRegex = new Regex("weibo.com");
            Regex sohuRegex = new Regex("t.sohu.com");
            Assert.IsTrue(sinaRegex.IsMatch(sinaUrl));
            Assert.IsTrue(sohuRegex.IsMatch(sohuUrl));
            Assert.IsFalse(sinaRegex.IsMatch(sohuUrl));
            Assert.IsFalse(sohuRegex.IsMatch(sinaUrl));

            var wc = new WebClient();
            var userpagestring = wc.DownloadString("http://t.sohu.com/m/2069717999"); // "UID = 645910"
            Regex reg = new Regex(@"UID = (\d+)");
            var matches = reg.Matches(userpagestring);

            var result = new WeiBeeCommon.DataTypes.SinaUser();
            result.id = matches[0].Groups[1].Value;
        }

        [TestMethod]
        public void GetWeiBeeTypeTest()
        {
            Form1 target = new Form1(); 
            Assert.AreEqual(target.GetWeiBeeType("http://weibo.com/drshipeng"),WeiBeeType.Sina);
            Assert.AreEqual(target.GetWeiBeeType("http://zengfuhu.t.sohu.com/"), WeiBeeType.Sohu);
        }
        
    }
}
